package com.asm.util;


import com.alibaba.fastjson.JSONObject;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;


/**
 * @author Yu
 */
public class FileFr {

    /**
     * 文件形成
     *
     * @param path     path
     * @param name     name
     * @param suffix   suffix
     * @param textarea textarea
     * @return String
     */
    public static String formation(String path, String name, String suffix, String textarea) {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd");
        String fileName = ft.format(date) + "-" + name + suffix;
        try {
            File inputFile = new File(path + fileName);
            inputFile.setReadable(true);
            inputFile.setWritable(true);
            FileOutputStream fileOutputStream = new FileOutputStream(inputFile);
            if (!inputFile.exists()) {
                boolean newFile = inputFile.createNewFile();
                System.out.println(newFile);
            }
            byte[] contentInBytes = textarea.getBytes();
            fileOutputStream.write(contentInBytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
    /**
     * 文件读取
     *
     * @param filePath filePath
     * @return List<String>
     */
    public static List<String> read(String filePath) {
        List<String> result = new ArrayList<>();
        try {
            File file = new File(filePath);
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s;
            while ((s = br.readLine()) != null) {
                result.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 文件上传
     *
     * @param path   path
     * @param name   name
     * @param suffix suffix
     * @param file   file
     * @return String
     */
    public static String upload(String path, String name, String suffix, MultipartFile file) {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd");
        String newFileName = ft.format(date) + "-" + name + suffix;
        try {
            File uploadFile = new File(path + newFileName);
//            file.transferTo(uploadFile);
            byte[] bytes = file.getBytes();
            FileOutputStream fileOutputStream = new FileOutputStream(uploadFile);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return newFileName;
    }

    public static MultipartFile fileMultipartFile (File file) throws IOException {
        MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
        return cMultiFile;
    }

    public static String getFileBase64(String path) throws IOException{
        Base64.Encoder encoder = Base64.getEncoder();
        JSONObject result = new JSONObject();
        try {
            File file = new File(path);
            OutputStream out = null;
            try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
                 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            ) {
                int len;
                byte[] buffer = new byte[1024];
                //每次读取的字符串长度，如果为-1，代表全部读取完毕
                //使用输入流从buffer里把数据读取出来
                while ((len = br.read(buffer)) != -1) {
                    //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
                    outStream.write(buffer, 0, len);
                }
//                // 对字节数组Base64编码
//                Base64Encoder base64 = new Base64Encoder();
                String encodedText  = encoder.encodeToString(outStream.toByteArray());
                result.put("base64",encodedText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String base64text = result.getString("base64");
        return base64text;
    }

}
